/**
 * Alibaba.com.cn Inc.
 * Copyright (c) 2004-2021 All Rights Reserved.
 */
package com.hiv.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * @author zhanghong
 * @version HttpRequestUtils.java, v 0.1 2022/5/30 17:11 下午 HttpRequestUtils Exp $$
 */
@Slf4j
public class HttpRequestUtils {
    /**
     * 发送get请求 返回byte
     * @param url    路径
     * @return
     */
    public static byte[] httpGetFile(String url){
        //get请求返回结果
        byte[] byteResult = null;
        try {

            CloseableHttpClient client = HttpClients.createDefault();
            //发送get请求
            HttpGet request = new HttpGet(url);
            CloseableHttpResponse response = client.execute(request);
            try {
                /**请求发送成功，并得到响应**/
                if (response.getStatusLine().getStatusCode() == 200) {
                    byteResult = EntityUtils.toByteArray(response.getEntity());
                } else {
                    log.error("get请求提交失败:" + url);
                }
            } finally {
                response.close();
            }

        } catch (IOException e) {
            log.error("get请求提交失败:" + url, e);
        }
        return byteResult;
    }
}
